﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace VechileABS
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();

        }
        SocketHelper socketClient;

        private void button1_Click(object sender, EventArgs e)
        {
            int port;
            int.TryParse(textBox2.Text, out port);
            socketClient = new SocketHelper(textBox1.Text, port);
            socketClient.SendInfor += new SocketHelper.SendEventHandler(SetClientState2);
            socketClient.StartClient();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Init();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Start();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            GetDataCmd();
        }

        private void Init()
        {
            string hphm = "LH17HCKJF44H043378";
            byte[] data = DataPack("I", hphm.PadRight(20, ' '));
            SetClientState(data);
            socketClient.SendMessage(data);
        }

        private void Start()
        {
            string dd = "500";
            string zero = "   0";
            string qdlx = "4X2后驱后驻车";
            StringBuilder msg = new StringBuilder();
            List<byte> cmd = new List<byte>();
            msg.Append("YYNNNN");//1~6:是否检测n轴制动
            msg.Append("NYNN");//7~10:1,2,3,4轴手刹
            msg.Append("Y");//11:整车手刹
            msg.Append("Y");//12:整车制动
            msg.Append("YYNNNN");//13~18:是否检测n轴ABS
            msg.Append(dd.PadLeft(5, ' '));//19~23:整备质量
            msg.Append(dd.PadLeft(5, ' '));//24~28:总质量
            msg.Append(dd.PadLeft(5, ' '));//29~33:载质量
            msg.Append("2");//34:主车轴数
            msg.Append(dd.PadLeft(4, ' '));//35~38:一轴左轮质量
            msg.Append(dd.PadLeft(4, ' '));//39~42:一轴右轮质量
            msg.Append(dd.PadLeft(4, ' '));//43~46:二轴左轮质量
            msg.Append(dd.PadLeft(4, ' '));//47~50:二轴右轮质量
            msg.Append(zero);//51~54:三轴左轮质量
            msg.Append(zero);//55~58:三轴右轮质量
            msg.Append(zero);//59~62:四轴左轮质量
            msg.Append(zero);//63~66:四轴右轮质量
            msg.Append(zero);//67~70:五轴左轮质量
            msg.Append(zero);//71~74:五轴右轮质量
            msg.Append(zero);//75~78:六轴左轮质量
            msg.Append(zero);//79~82:六轴右轮质量
            msg.Append("0");//83:是否是双转向轴
            msg.Append("1");//84:车辆类型
            msg.Append("1");//85:ABS型号
            msg.Append(qdlx.PadRight(26, ' '));//86~116:驱动形式
            byte[] data = DataPack("T", msg.ToString());
            SetClientState(data);
            SetClientState(Encoding.Default.GetString(data,5,117));
            socketClient.SendMessage(data);
        }

        private void GetDataCmd()
        {
            byte[] data = DataPack("D", "");
            SetClientState(data);
            socketClient.SendMessage(data);
        }

        public void DataAnalysis(byte[] buf)
        {
            if (buf.Length>6)
            {
                byte lcr = DataLCR(buf);
                if (lcr == buf[buf.Length-2])
                {
                    string cmd = Encoding.Default.GetString(buf, 4, buf.Length - 6);
                    Invoke(new System.Windows.Forms.MethodInvoker(delegate
                    {
                        richTextBox1.AppendText(DateTime.Now.ToString() + "：" + cmd + "\n\n");
                        richTextBox1.ScrollToCaret();
                    }));

                    if (cmd.Contains("I"))
                    {
                        Invoke(new System.Windows.Forms.MethodInvoker(delegate
                        {
                            Start();
                        }));
         
                    }
                    else if (cmd.Contains("T0"))
                    {
                        Invoke(new System.Windows.Forms.MethodInvoker(delegate
                        {
                            GetDataCmd();
                        }));
                
                    }
                    else if (cmd.Contains("DD"))
                    {
                        if (buf.Length == 616)
                        {
                            #region MyRegion
                            string data = Encoding.Default.GetString(buf, 4, 608);
                            Invoke(new System.Windows.Forms.MethodInvoker(delegate
                            {
                                richTextBox1.AppendText(DateTime.Now.ToString() + "：" + data + "\n\n");
                                richTextBox1.ScrollToCaret();
                            }));

                            string value;
                            #region 一轴
                            value = data.Substring(2, 5);//2~6：一轴左轮重
                            value = data.Substring(7, 5);//7~11：一轴右轮重
                            value = data.Substring(12, 5);//12~16：一轴轴重
                            value = data.Substring(17, 5);//17~21：一轴制和左轮制动(10N)
                            value = data.Substring(22, 5);//22~26：一轴制和右轮制动(10N)
                            value = data.Substring(27, 5);//27~31：一轴制动因数(%)
                            value = data.Substring(32, 1);//32：一轴制动因数评价
                            value = data.Substring(33, 5);//33~37：一轴制差左轮制动(10N)
                            value = data.Substring(38, 5);//38~42：一轴制差右轮制动(10N)
                            value = data.Substring(43, 5);//43~47：一轴制动力差占轴荷(%)
                            value = data.Substring(48, 1);//48：一轴制动力差占轴荷评价
                            value = data.Substring(49, 5);//49~53：一轴左阻滞力
                            value = data.Substring(54, 1);//54：一轴左阻滞力评价
                            value = data.Substring(55, 5);//55~59：一轴右阻滞力
                            value = data.Substring(60, 1);//60：一轴右阻滞力评价
                            value = data.Substring(61, 5);//61~65：一轴协调时间
                            value = data.Substring(66, 1);//66：一轴协调时间评价
                            value = data.Substring(67, 1);//67：一轴制动总评价
                            value = data.Substring(68, 1);//68：一轴ABS左阀的测试评价
                            value = data.Substring(69, 1);//69：一轴ABS右阀的测试评价
                            value = data.Substring(70, 1);//70：一轴ABS阀是否装反评价
                            value = data.Substring(71, 1);//71：一轴ABS左速度的测试评价
                            value = data.Substring(72, 1);//72：一轴ABS右速度的测试评价
                            value = data.Substring(73, 1);//73：一轴ABS速度是否装反评价
                            value = data.Substring(74, 1);//74：一轴ABS左传感器齿圈的测试评价
                            value = data.Substring(75, 1);//75：一轴ABS右传感器齿圈的测试评价
                            value = data.Substring(76, 1);//76：一轴ABS总评价
                            value = data.Substring(77, 2);//77~78：一轴ABS左电磁阀故障
                            value = data.Substring(79, 2);//79~80：一轴ABS右电磁阀故障
                            value = data.Substring(81, 2);//81~82：一轴ABS左传感器故障
                            value = data.Substring(83, 2);//83~84：一轴ABS右传感器故障 
                            #endregion

                            #region 二轴
                            value = data.Substring(85, 5);//2~6：二轴左轮重
                            value = data.Substring(90, 5);//7~11：二轴右轮重
                            value = data.Substring(95, 5);//12~16：二轴轴重
                            value = data.Substring(100, 5);//16~21：二轴制和左轮制动(10N)
                            value = data.Substring(105, 5);//22~26：二轴制和右轮制动(10N)
                            value = data.Substring(110, 5);//27~31：二轴制动因数(%)
                            value = data.Substring(115, 1);//32：二轴制动因数评价
                            value = data.Substring(116, 5);//33~37：二轴制差左轮制动(10N)
                            value = data.Substring(121, 5);//38~42：二轴制差右轮制动(10N)
                            value = data.Substring(126, 5);//43~47：二轴制动力差占轴荷(%)
                            value = data.Substring(131, 1);//48：二轴制动力差占轴荷评价
                            value = data.Substring(132, 5);//49~53：二轴左阻滞力
                            value = data.Substring(137, 1);//54：二轴左阻滞力评价
                            value = data.Substring(138, 5);//55~59：二轴右阻滞力
                            value = data.Substring(143, 1);//60：二轴右阻滞力评价
                            value = data.Substring(144, 5);//61~65：二轴协调时间
                            value = data.Substring(149, 1);//66：二轴协调时间评价
                            value = data.Substring(150, 1);//67：二轴制动总评价
                            value = data.Substring(151, 1);//68：二轴ABS左阀的测试评价
                            value = data.Substring(152, 1);//69：二轴ABS右阀的测试评价
                            value = data.Substring(153, 1);//70：二轴ABS阀是否装反评价
                            value = data.Substring(154, 1);//71：二轴ABS左速度的测试评价
                            value = data.Substring(155, 1);//72：二轴ABS右速度的测试评价
                            value = data.Substring(156, 1);//73：二轴ABS速度是否装反评价
                            value = data.Substring(157, 1);//74：二轴ABS左传感器齿圈的测试评价
                            value = data.Substring(158, 1);//75：二轴ABS右传感器齿圈的测试评价
                            value = data.Substring(159, 1);//76：二轴ABS总评价
                            value = data.Substring(160, 2);//77~78：二轴ABS左电磁阀故障
                            value = data.Substring(162, 2);//79~80：二轴ABS右电磁阀故障
                            value = data.Substring(164, 2);//81~82：二轴ABS左传感器故障
                            value = data.Substring(166, 2);//83~84：二轴ABS右传感器故障 
                            #endregion

                            value = data.Substring(501, 2);//501~502：ASR阀的故障
                            value = data.Substring(503, 5);//503~507：二轴手刹轴重
                            value = data.Substring(508, 5);//508~512：二轴手刹制和左轮制动(10N)
                            value = data.Substring(513, 5);//513~517：二轴手刹制和右轮制动(10N)
                            value = data.Substring(518, 5);//518~522：二轴手刹制和
                            value = data.Substring(523, 1);//523：二轴手刹总评价

                            value = data.Substring(566, 5);//566~570：整车轴重
                            value = data.Substring(571, 5);//571~575：整车制和左轮制动(10N)
                            value = data.Substring(576, 5);//576~580：整车制和右轮制动(10N)
                            value = data.Substring(581, 5);//581~585：整车制和
                            value = data.Substring(586, 1);//586：整车制动评价
                            value = data.Substring(587, 5);//587~591：整车手刹轴重
                            value = data.Substring(592, 5);//592~596：整车手刹制和左轮制动(10N)
                            value = data.Substring(597, 5);//597~601：整车手刹制和右轮制动(10N)
                            value = data.Substring(602, 5);//602~606：整车手刹制和
                            value = data.Substring(607, 1);//607：整车手刹制动评价
                            #endregion

                        }
                    }
                    else if (cmd.Contains("T5"))
                    {
                    }
                }
            }




        }

        public byte[] DataPack(string command,string content)
        {
            byte[] datacommand = Encoding.Default.GetBytes(command);
            byte[] data = Encoding.Default.GetBytes(content);
            byte[] datalength = new byte[2];
            UInt16 result = (UInt16)(data.Length + 1);
            datalength[0] = (byte)(result & 0xff);//低位
            datalength[1] = (byte)(result >> 8);//高位


            List<byte> cmd = new List<byte>();
            cmd.Add(0x02);
            cmd.Add(0x01);
            cmd.AddRange(datalength);
            cmd.AddRange(datacommand);
            cmd.AddRange(data);
            cmd.Add(DataLCR(datalength, datacommand, data));
            cmd.Add(0x03);
            byte[] array = new byte[cmd.Count];
            cmd.CopyTo(array);
            return array;
        }

        /// <summary>
        /// LCR校验
        /// </summary>
        /// <param name="datalength"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public byte DataLCR(byte[] datalength, byte[] datacommand, byte[] data)
        {
            byte uSum = 0x01;
            for (int i = 0; i < datalength.Length; i++)
            {
                uSum += (byte)(datalength[i]);
            }
            for (int i = 0; i < datacommand.Length; i++)
            {
                uSum += (byte)(datacommand[i]);
            }
            for (int i = 0; i < data.Length; i++)
            {
                uSum += (byte)(data[i]);
            }

            return uSum;

        }

        public byte DataLCR(byte[] data)
        {
            byte uSum =new byte();
            for (int i = 1; i < data.Length-2; i++)
            {
                uSum += (byte)(data[i]);
            }

            return uSum;

        }

        private void SetClientState(byte[] data)
        {
            string msg = "";
            for (int i = 0; i < data.Length; i++)
            {
                msg += string.Format("{0:X}", data[i]).PadLeft(2, '0')+" ";
            }

            richTextBoxInfor.AppendText(DateTime.Now.ToString() + "：" + msg + "\n\n");
            richTextBoxInfor.ScrollToCaret();
        }

        private void SetClientState(string msg)
        {
            richTextBoxInfor.AppendText(DateTime.Now.ToString() + "：" + msg + "\n\n");
            richTextBoxInfor.ScrollToCaret();
        }

        private void SetClientState2(byte[] data)
        {
            if (data.Length>0)
            {
                string msg = "";
                for (int i = 0; i < data.Length; i++)
                {
                    msg += string.Format("{0:X}", data[i]).PadLeft(2, '0') + " ";
                }
                 Invoke(new System.Windows.Forms.MethodInvoker(delegate
                     {
                         richTextBox1.AppendText(DateTime.Now.ToString() + "：" + msg + "\n\n");
                         richTextBox1.ScrollToCaret();
                     }));

                 DataAnalysis(data);
            }

        }

        private void SetClientState2(string msg)
        {
            richTextBox1.AppendText(DateTime.Now.ToString() + "：" + msg + "\n\n");
            richTextBox1.ScrollToCaret();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Init();
        }

    }

}
